package com.platform.utils;

import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/5/18 0018.
 */
public class RunResult<T>  implements Serializable {
    public static final String DEFAULT_OK = "成功";
    public static final String DEFAULT_FAIL = "失败";



    private Integer statusCode;
    private String message;
    private T data;


    public static RunResult ok() {
        return ok(null);
    }

    public static <T> RunResult ok(T data) {
        return ok(HttpStatus.OK, data);
    }

    public static <T> RunResult ok(T data, String message) {
        return ok(HttpStatus.OK.value(), data, message);
    }

    public static <T> RunResult ok(HttpStatus statusCode, T data) {
        return ok(statusCode.value(), data);
    }

    public static <T> RunResult ok(Integer statusCode, T data) {
       return ok(statusCode, data, DEFAULT_OK);
    }

    public static <T> RunResult ok(Integer statusCode, T data, String message) {

        RunResult<T> runResult = new RunResult<T>();
        runResult.setStatusCode(statusCode);
        runResult.setMessage(message);
        runResult.setData(data);
        return runResult;
    }

    public static RunResult fail(Integer statusCode, String message) {
        return fail(statusCode, message, null);
    }

    public static RunResult fail(HttpStatus statusCode, String message) {
        return fail(statusCode.value(), message);
    }

    public static RunResult fail(Integer statusCode, Exception exception) {
        return fail(statusCode, exception.getMessage());
    }

//    public static RunResult fail(HttpStatus statusCode, Exception exception) {
//        if (exception != null && StringUtils.isNotEmpty(exception.getMessage())) {
//            return fail(statusCode.value(), exception.getMessage());
//        } else {
//            return fail(statusCode.value(), ExceptionUtils.getStackTrace(exception));
//        }
//    }

    public static RunResult fail(Integer statusCode) {
        return fail(statusCode, DEFAULT_FAIL);
    }

    public static RunResult fail(HttpStatus statusCode) {
        return fail(statusCode.value(), DEFAULT_FAIL);
    }

    public static <T> RunResult<T> fail(HttpStatus statusCode, String message, T data) {
        return fail(statusCode.value(), message, data);
    }

    public static <T> RunResult<T> fail(Integer statusCode, String message, T data) {
        RunResult runResult = new RunResult();
        runResult.setStatusCode(statusCode);
        runResult.setMessage(message);
        runResult.setData(data);
        return runResult;
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Boolean isSuccess() {
        return !(HttpStatus.valueOf(this.statusCode).is4xxClientError() || HttpStatus.valueOf(this.statusCode).is5xxServerError());
    }
}
